package com.hongxin.nw.netty.factory;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.analysis.ASDU;
import com.hongxin.nw.netty.mq_info.*;
import com.hongxin.nw.netty.utils.JSONUtils;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import lombok.extern.slf4j.Slf4j;

/**
 * @remark 发送数据到MQ队列
 * @author: GuoXiaolin
 * @date 2019/6/21 16:28
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class MqFactory {

    public static MqFactory getInstance() {
        return new MqFactory();
    }

    public JSONObject sendData(Transfer transfer) throws Exception{
        if (transfer.isNeedMq()){
            ASDU asdu = transfer.getAsdu();
            if (asdu.getType() == TypeIdentif.IDENTIFY_M_MD_NA_1.getInt()){//132
                if (asdu.getCot() == TypeIdentif.COT_SUMMON.getInt() || asdu.getCot() == TypeIdentif.COT_SPONTANEOUS.getInt()){
                    //MQ 响应总召 / 突发(自发), 设备功率信息不发送 MQ
                    if (transfer.getEType() == TypeIdentif.EQUIPMENT_DIRECT.getInt()){
                        Mq mq = new Mq(new EquipTypeInfo());
                        return mq.sendMq(transfer);
                    }else{
                        Mq mq = new Mq(new ExEquipTypeInfo());
                        return mq.sendMq(transfer);
                    }
                }
            }
            if (asdu.getType() == TypeIdentif.IDENTIFY_M_SP_NA_1.getInt()){//1
                if (asdu.getCot() == TypeIdentif.COT_SUMMON.getInt() || asdu.getCot() == TypeIdentif.COT_SPONTANEOUS.getInt()){
                    //MQ 响应总召 / 突发(自发)
                    if (transfer.getEType() == TypeIdentif.EQUIPMENT_DIRECT.getInt()){
                        Mq mq = new Mq(new ChargingStatus());
                        return  mq.sendMq(transfer);
                    }else{
                        Mq mq = new Mq(new ExChargingStatus());
                        return  mq.sendMq(transfer);
                    }
                }
            }
            if (asdu.getType() == TypeIdentif.IDENTIFY_M_ME_NB_1.getInt()){//11
                if (asdu.getCot() == TypeIdentif.COT_SUMMON.getInt() || asdu.getCot() == TypeIdentif.COT_SPONTANEOUS.getInt()){
                    //MQ 响应总召 / 突发(自发)
                    if (transfer.getEType() == TypeIdentif.EQUIPMENT_DIRECT.getInt()){
                        Mq mq = new Mq(new ChargeInfos());
                        return  mq.sendMq(transfer);
                    }else{
                        Mq mq = new Mq(new ExChargeInfos());
                        return  mq.sendMq(transfer);
                    }
                }
            }
            if (asdu.getType() == TypeIdentif.IDENTIFY_M_RE_NA_1.getInt()){//130
                return sendMq130(transfer);
            }
            if (asdu.getType() == TypeIdentif.IDENTIFY_M_JC_NA_1.getInt()){//134
                return sendMq134(transfer);
            }
            if (asdu.getType() == TypeIdentif.IDENTIFY_M_IT_NA_1.getInt()){ //15
                if (asdu.getCot() == TypeIdentif.COT_SUMMON_C.getInt()){
                    //MQ 充电设备收到计数量总召命令后已上传累积量
//                    List<DTmitna1> myl = (List<DTmitna1>)
                    return JSONUtils.getRoot(-1,StaticValue.VAL_1, StaticValue.VAL_NULL);
                }
            }
            if (asdu.getType() == TypeIdentif.IDENTIFY_C_CS_NA_1.getInt()){//103
                if (asdu.getCot()  == TypeIdentif.COT_ACTIVATION_CONFIRM.getInt()){
                    //MQ 时钟同步已确认
                    return JSONUtils.getRoot(-1,StaticValue.VAL_2, StaticValue.VAL_NULL);
                }
            }
            if (asdu.getType() == TypeIdentif.IDENTIFY_C_SD_NA_1.getInt()){//133
                if (asdu.getCot() == TypeIdentif.COT_ACTIVATION_CONFIRM.getInt()){
                    //MQ 远程升级已确认
                    return JSONUtils.getRoot(-1,StaticValue.VAL_3, StaticValue.VAL_NULL);
                }
            }
            if (asdu.getType() == TypeIdentif.IDENTIFY_C_CI_NA_1.getInt()){//101
                if (asdu.getCot() == TypeIdentif.COT_ACTIVATION_CONFIRM.getInt()){
                    //MQ 计数量总召已确认
                    return JSONUtils.getRoot(-1,StaticValue.VAL_4, StaticValue.VAL_NULL);
                }
                if (asdu.getCot() == TypeIdentif.COT_ACTIVATION_TERMINATION.getInt()){
                    //MQ 计数量总召已结束
                    return JSONUtils.getRoot(-1,StaticValue.VAL_5, StaticValue.VAL_NULL);
                }
            }
            if (asdu.getType() == TypeIdentif.IDENTIFY_C_IC_NA_1.getInt()){//100
                if (asdu.getCot()== TypeIdentif.COT_ACTIVATION_TERMINATION.getInt()){
                    //MQ 总召已结束
                    return JSONUtils.getRoot(-1,StaticValue.VAL_6, StaticValue.VAL_NULL);
                }
                if (asdu.getCot()== TypeIdentif.COT_ACTIVATION_CONFIRM.getInt()){
                    //MQ 总召已确认
                    return JSONUtils.getRoot(-1,StaticValue.VAL_7, StaticValue.VAL_NULL);
                }
            }
        }
        return JSONUtils.getRoot(-1,StaticValue.VAL_8, StaticValue.VAL_NULL);
    }

    //130 解析充电设备业务数据
    private JSONObject sendMq130(Transfer transfer) throws Exception {
        if (TypeIdentif.M_RECORD_START_CHARGE.getInt() == transfer.getRecord()){
            //远程启动充电执行结果
            Mq mq = new Mq(new StartCharge());
            return mq.sendMq(transfer);
        }
        if (TypeIdentif.M_RECORD_UPLOAD_DATA.getInt() == transfer.getRecord()){
            //充电记录上传数据
            Mq mq = new Mq(new EndCharge());
            return mq.sendMq(transfer);
        }
        if (TypeIdentif.M_RECORD_CHARGE_PROCESS.getInt() == transfer.getRecord()){
            //充电过程中上传数据
            Mq mq = new Mq(new ChargeProcessStatus());
            return mq.sendMq(transfer);
        }
        if (TypeIdentif.M_RECORD_AUTH.getInt() == transfer.getRecord()){
            //设备请求鉴权
            return JSONUtils.getRoot(-1,StaticValue.VAL_9, StaticValue.VAL_NULL);
        }
        if (TypeIdentif.M_RECORD_REQUEST_BILLING.getInt() == transfer.getRecord()){
            //设备请求请求计费模型下发
            return JSONUtils.getRoot(-1,StaticValue.VAL_10, StaticValue.VAL_NULL);
        }
        if (TypeIdentif.M_RECORD_BLACK_LIST.getInt() == transfer.getRecord()){
            //设备通知更新充电设备的黑名单状态
            return JSONUtils.getRoot(-1,StaticValue.VAL_11, StaticValue.VAL_NULL);
        }
        if (TypeIdentif.M_RECORD_KW_CONTROLLER.getInt() == transfer.getRecord()){
            //远程功率控制
            return JSONUtils.getRoot(-1,StaticValue.VAL_12, StaticValue.VAL_NULL);
        }
        if (TypeIdentif.M_RECORD_BILLING.getInt() == transfer.getRecord()){
            //返回 A.9下发计费模型结果数据
            return JSONUtils.getRoot(-1,StaticValue.VAL_13, StaticValue.VAL_NULL);
        }
        if (TypeIdentif.M_RECORD_SET_PARAM.getInt() == transfer.getRecord()){
            //A.36充电设备数据上报参数设置结果
            return JSONUtils.getRoot(-1,StaticValue.VAL_14, StaticValue.VAL_NULL);
        }
        if (TypeIdentif.M_RECORD_END_CHARGE.getInt() == transfer.getRecord()){
            //远程终止充电确认
            return JSONUtils.getRoot(-1,StaticValue.VAL_15, StaticValue.VAL_NULL);
        }
        if (TypeIdentif.M_RECORD_UPGRADE.getInt() == transfer.getRecord()){
            //远程升级启动
            return JSONUtils.getRoot(-1,StaticValue.VAL_16, StaticValue.VAL_NULL);
        }
        return JSONUtils.getRoot(-1,StaticValue.VAL_17, StaticValue.VAL_NULL);
    }

    //134 解析充电设备实时监测数据
    private JSONObject sendMq134(Transfer transfer) {
        //交流设备实时监测数据
        if (transfer.getEType() == TypeIdentif.EQUIPMENT_COMMUNICATION.getInt()){
            return JSONUtils.getRoot(-1,StaticValue.VAL_18, StaticValue.VAL_NULL);
        }
        //直流设备实时监测数据
        if (transfer.getEType() == TypeIdentif.EQUIPMENT_DIRECT.getInt()){
            return JSONUtils.getRoot(-1,StaticValue.VAL_19, StaticValue.VAL_NULL);
        }
        return JSONUtils.getRoot(-1,StaticValue.VAL_20, StaticValue.VAL_NULL);
    }
}
